package com.eightywork.life.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.eightywork.life.R;
import com.eightywork.life.activity.BrowserActivity;
import com.eightywork.life.imageCache.ImageLoader;
import com.eightywork.life.model.Coupon;
import com.eightywork.life.service.BaseService;
import com.eightywork.life.service.DataService;
import com.eightywork.life.util.AndroidUtil;
import com.eightywork.life.util.FuncUtil;

public class MyCouponListAdapter extends BaseAdapter {

	private int mcResource = R.layout.item_service_event;
	private LayoutInflater mcInflater;
	private Context mcContext;
	private ImageLoader imageLoader;
	private List<Coupon> list;

	public MyCouponListAdapter(Context context, List<Coupon> list) {
		mcContext = context;
		mcInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoader = new ImageLoader(context);
		
		this.list = list;
		// TODO Auto-generated constructor stub
	}
	
	
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}


	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return (list != null)?list.size():0;
	}


	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final ViewHolder holder;
		final Coupon myCoupon = list.get(position);
		if (convertView == null) {
			holder = new ViewHolder();
			convertView  = mcInflater.inflate(mcResource, parent, false);
			holder.username = (TextView)convertView.findViewById(R.id.name);
			holder.name = (TextView)convertView.findViewById(R.id.append_name);
		//	holder.status = (TextView)convertView.findViewById(R.id.tv_state);
			holder.time = (TextView)convertView.findViewById(R.id.time);
			holder.content = (TextView)convertView.findViewById(R.id.content);
			holder.subject = (TextView)convertView.findViewById(R.id.append_title);
			holder.avatar = (ImageView)convertView.findViewById(R.id.avatar);
			holder.verifyType = (ImageView)convertView.findViewById(R.id.verify_type);
			holder.image = (ImageView)convertView.findViewById(R.id.append_image);		
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder)convertView.getTag();
		}
		holder.content.setText(myCoupon.getContent());
		holder.username.setText(myCoupon.getBiz_name());
		holder.name.setText("@" + myCoupon.getBiz_name());
		holder.subject.setText(myCoupon.getSubject());
		holder.time.setText(FuncUtil.timeToText(Long.valueOf(myCoupon.getDateline()) * 1000L));
		if (myCoupon.getPic_thumb().startsWith("http:")) {
			imageLoader.DisplayImage(myCoupon.getPic_thumb(), holder.image, 0, 0, 0);
		} else  {
			imageLoader.DisplayImage(BaseService.DOMAIN + myCoupon.getPic_thumb(), holder.image, 0, 0, 0);
		}
		
		int len = AndroidUtil.dpToPx(48, mcContext);
		imageLoader.DisplayImage(myCoupon.getBiz_avatar_middle(), holder.avatar, 0, len, len);
		holder.verifyType.setBackgroundResource(DataService.getVerifyTypeRes(myCoupon.getBiz_verify_type()));
		  convertView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intent  = new Intent(mcContext, BrowserActivity.class);
					intent.putExtra("url", BaseService.DOMAIN
							+ myCoupon.getAppend_link());
					intent.putExtra("idtype", "feed");
				    mcContext.startActivity(intent);
				}
			});
		return convertView;
	}


	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}
	public final class ViewHolder {
		private TextView username;
		private ImageView avatar;
		private TextView  status;
		private ImageView image;
		private ImageView verifyType;
		private TextView time;
		private TextView subject;
		private TextView content;
		private TextView name;
		
		
	}

}
